#include "library.h"
#include <algorithm>

void Library::add_book(Book book)
{
    Books.push_back(book);
    cout << "Added:" << book.get_name() << endl;
}

void Library::delete_book(int book_ID)
{
    vector<Book>::iterator it = std::find_if(Books.begin(), Books.end(), [book_ID](Book& book) {return book.get_ID() == book_ID;});
    if (it != Books.end()) {
        cout << "Deleted:" << (*it).get_name() << endl;
        Books.erase(it);
    }
    else {
        cout << "Delete failed" << endl;
    }
}

void Library::find_book(int book_ID)
{
    vector<Book>::iterator it = std::find_if(Books.begin(), Books.end(), [book_ID](Book& book) {return book.get_ID() == book_ID;});
    if (it != Books.end()) {
        (*it).print_message();
    }
    else {
        cout << "Find Failed" << endl;
    }
}

void Library::borrow_book(int book_ID, string borrower)
{
    vector<Book>::iterator it = std::find_if(Books.begin(), Books.end(), [book_ID](Book& book) {return book.get_ID() == book_ID;});
    if (it != Books.end()) {
        cout << "Borrowed:" << (*it).get_name() << endl;
        (*it).change_state();
    }
    else {
        cout << "Borrow Failed" << endl;
    } 
}

void Library::return_book(int book_ID)
{
    vector<Book>::iterator it = std::find_if(Books.begin(), Books.end(), [book_ID](Book& book) {return book.get_ID() == book_ID;});
    if (it != Books.end()) {
        cout << "Returned:" << (*it).get_name() << endl;
        (*it).change_state();
    }
    else {
        cout << "Return Failed" << endl;
    }
}